package it.bela.pgcdc.service.impl;

import java.util.List;

import it.bela.pgcdc.common.exception.ServiceException;
import it.bela.pgcdc.common.utils.Utilities;
import it.bela.pgcdc.dao.PreferenzaDao;
import it.bela.pgcdc.domain.Preferenza;
import it.bela.pgcdc.service.PreferenzaService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
@Service
@Transactional(rollbackFor = ServiceException.class)
public class PreferenzaServiceImpl implements PreferenzaService {
	
	private static final Logger log = Logger.getLogger(PreferenzaServiceImpl.class);
	
	@Autowired(required = true)
	private PreferenzaDao preferenzaDao;
	
	private boolean hasValidKey(Preferenza preferenza) {
		return (preferenza.getId() != 0);
	}

	@Override
	public List<Preferenza> findByExample(Preferenza preferenza) throws ServiceException {
		
		try {
			log.debug("Start list Preferenza");
			
			preferenza = (Preferenza) Utilities.setDefaultParametersSelect(preferenza);
			
			List<Preferenza> list = preferenzaDao.findByExample(preferenza);
			
			log.debug("End list Preferenza");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample Preferenza failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public List<Preferenza> list() throws ServiceException {
		
		try {
			log.debug("Start list");
			
			Preferenza preferenza = new Preferenza();
			
			preferenza = (Preferenza) Utilities.setDefaultParametersSelect(preferenza);

			List<Preferenza> list = preferenzaDao.findByExample(preferenza);
			
			log.debug("End list");
			
			return list;

		} catch (Exception e) {
			log.debug("findByExample failed" + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean delete(Preferenza preferenza) throws ServiceException {
		log.debug(preferenza.getClass().toString() + " - Start delete");
		
		String message = preferenza.getClass().toString() + " - delete Preferenza failed.";
		try {
			if (!hasValidKey(preferenza)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			preferenza = (Preferenza) preferenzaDao.findById(preferenza.getClass(), preferenza.getId());
			preferenzaDao.delete(preferenza);
			
			log.debug(preferenza.getClass().toString() + " - End delete Preferenza");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}

	public boolean persist(Preferenza preferenza) throws ServiceException {
		log.debug(preferenza.getClass().toString() + " - Start persist Preferenza");
		
		String message = preferenza.getClass().toString() + " - persist Preferenza failed.";
		try {
			
			preferenza = (Preferenza) Utilities.setDefaultParametersInsert(preferenza);
			preferenzaDao.persist(preferenza);
			
			log.debug(Preferenza.class.toString() + " - End persist Preferenza");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean save(Preferenza preferenza) throws ServiceException {
		log.debug(preferenza.getClass().toString() + " - Start save Preferenza");
		
		String message = Preferenza.class.toString() + " - save Preferenza failed.";
		try {
			
			preferenza = (Preferenza) Utilities.setDefaultParametersInsert(preferenza);
			preferenzaDao.save(preferenza);
			
			log.debug(Preferenza.class.toString() + " - End save Preferenza");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public boolean merge(Preferenza preferenza) throws ServiceException {
		log.debug(Preferenza.class.toString() + " - Start merge Preferenza");
		
		String message = Preferenza.class.toString() + " - merge Preferenza failed.";
		try {
			if (!hasValidKey(preferenza)) {
				String messageInner = message + " Oggetto senza id.";
				log.debug(messageInner);
				throw new ServiceException(messageInner);
			}
			
			preferenza = (Preferenza) Utilities.setDefaultParametersUpdate(preferenza);
			preferenzaDao.merge(preferenza);
			
			log.debug(Preferenza.class.toString() + " - End merge Preferenza");
			return true;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
	
	public Preferenza findById(Class<Preferenza> classe, Long id)throws ServiceException {
		log.debug(Preferenza.class.toString() + " - Start findById Preferenza");
		
		String message = Preferenza.class.toString() + " - findById Preferenza failed.";
		try {
			
			Preferenza preferenza = (Preferenza) preferenzaDao.findById(classe, id);
			
			log.debug(Preferenza.class.toString() + " - End findById Preferenza");
			return preferenza;
		} catch (Exception e) {
			log.debug(message + e.getMessage(), e);
			throw new ServiceException(e);
		}
	}
}
